package com.zjh.workflow.flowable.controller;


import com.zjh.admin.entity.VO.SysUserVO;
import com.zjh.common.entity.Response;
import com.zjh.workflow.flowable.entity.TaskInputParam;
import com.zjh.workflow.flowable.entity.vo.CommentVo;
import com.zjh.workflow.flowable.entity.vo.FlowNodeVo;
import com.zjh.workflow.flowable.entity.vo.TaskVo;
import com.zjh.workflow.flowable.entity.wrapper.FlowTaskDto;
import com.zjh.workflow.flowable.service.IFlowableCommentService;
import com.zjh.workflow.flowable.service.IFlowableTaskService;
import com.zjh.workflow.flowable.service.TaskHandleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api("流程实例")
@RestController
@RequestMapping("/WorkFlow/task-handle")
public class TaskHandleController {


    @Resource
    private IFlowableTaskService flowableTaskService;
    @Resource
    private TaskHandleService taskHandleService;

    @Resource
    private IFlowableCommentService flowableCommentService;

    /**
     * 获取待办
     */
    @PostMapping("/getTodoList")
    public Response<List<FlowTaskDto>> getTodoList(@RequestBody SysUserVO sysUserVO) {

        List<FlowTaskDto> res = taskHandleService.getTodoList(sysUserVO);

        return Response.success(res);
    }

    /**
     * 获取待办2
     */
    @PostMapping("/getTodoList2")
    public Response<?> getTodoList2(@RequestParam String userId) {
        List<TaskVo> applyingTasks = flowableTaskService.getApplyingTasks(userId);

        return Response.success(applyingTasks);
    }

    /**
     * 获取已办
     */
    @PostMapping("/getAppliedTasks")
    public Response<?> getAppliedTasks(@RequestParam String userId) {
        List<TaskVo> appliedTasks = flowableTaskService.getApplyedTasks(userId);

        return Response.success(appliedTasks);
    }

    @ApiModelProperty(notes = "根据 实例Id 进行流程审批")
    @PostMapping("/approve")
    public Response<Void> completeByProcessInstanceId(@RequestBody TaskInputParam taskInputParam) {

        taskHandleService.approveTask(taskInputParam);

        return Response.success();
    }

    @ApiModelProperty(notes = "根据 实例Id 进行流程驳回")
    @PostMapping("/reject")
    public Response<?> rejectByProcessInstanceId(@RequestBody TaskInputParam taskInputParam) {
        Response<String> response = taskHandleService.backToStepTask(taskInputParam);

        return response;
    }

    @ApiModelProperty(notes = "根据 实例Id 进行流程中止")
    @PostMapping("/stop")
    public Response<?> stopProcessInstanceById(@RequestBody TaskInputParam taskInputParam) {
        Response<String> response = taskHandleService.stopProcessInstanceById(taskInputParam);

        return response;
    }

    /**
     * 通过流程实例id获取审批意见
     * 需要审批用户存在于 ACT_ID_USER中，否则会查询不到
     */
    @GetMapping("/commentsByProcessInstanceId")
    public Response<List<CommentVo>> commentsByProcessInstanceId(String processInstanceId) {
        List<CommentVo> commentVos = flowableCommentService.getFlowCommentVosByProcessInstanceId(processInstanceId);
        return Response.success(commentVos);
    }


    /**
     * 获取可驳回节点列表
     */
    @GetMapping(value = "/getBackNodesByProcessInstanceId/{processInstanceId}/{taskId}")
    public Response<List<FlowNodeVo>> getBackNodesByProcessInstanceId(@PathVariable String processInstanceId, @PathVariable String taskId) {
        List<FlowNodeVo> res = flowableTaskService.getBackNodesByProcessInstanceId(processInstanceId, taskId);
        return Response.success(res);

    }


}
